package com.crm.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.crm.dao.UsersDao;
import com.crm.entity.Users;
import com.crm.service.UsersService;

@Service
public class UsersServiceImpl implements UsersService {

	@Autowired
	private UsersDao usersDao;

	@Override
	public Page<Users> getUsersByPage(Integer pageIndex, String name) {
		Users users = new Users();
		if (StringUtils.isNotBlank(name)) {
			users.setName(name);
		}
		users.setFlag(true);
		Example<Users> example = Example.of(users);
		PageRequest pageable = new PageRequest(pageIndex, 10);

		return usersDao.findAll(example, pageable);
	}

	@Override
	public void addUsers(Users users) {
		users.setFlag(true);
		if (StringUtils.isBlank(users.getPassword())) {
			users.setPassword("123456");
		}
		usersDao.save(users);
	}

	@Override
	public void deleteUsers(String id) {
		Users users = usersDao.findOne(id);
		users.setFlag(false);
		usersDao.save(users);
	}

	@Override
	public Users login(String username, String password) {
		Users users = usersDao.findByNameAndPassword(username, password);
		if (users != null) {
			return users;
		}
		return null;
	}

}
